<root xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="rds.html" />

<?python
    import os
    from mnemoply.utils import MonthYear, format_currency, format_date, getBarcodeImage, read_binary_file
    from mnemoply.common_knowledge import month_captions
?>

<report>
    <pages>
        <?python
            objs = [model.StudentInGroup.get(int(x)) for x in ids]
            objs_charged = [(sig, calcCharge(sig, MonthYear(charge_month, charge_year))) for sig in objs]
        ?>

        <page py:if="charged is not None" py:for="sig, charged in objs_charged">
            <customer py:content="sig.studentAgreement.customer.title" />
            <student py:content="sig.student.fullName" />
            <student_short>${sig.student.surname} ${sig.student.forename}</student_short>
            <group py:content="sig.group.shortName" />
            <month py:content="unicode(month_captions[charge_month]).lower()" />
            <debt py:content="format_currency(-sig.cacheBalance - charged.amount)" />
            <charged py:content="format_currency(charged.amount)" />
            <total py:content="format_currency(-sig.cacheBalance if sig.cacheBalance &lt; 0 else 0)" />
            <balance py:content="format_currency(sig.cacheBalance)" />
            <lessons py:content="calcLessons(sig, charged)" />
            <today py:content="format_date()" />
            <?python
                barcodes_dir = "barcodes"
                tmp_payment_type = "00"
                barcode_num = tmp_payment_type + str(sig.studentAgreement.id).rjust(5, "0")
                barcode_file = getBarcodeImage(barcode_num, barcodes_dir)

                from base64 import b64encode
                barcode_b64 = b64encode(read_binary_file("./" + barcode_file))
            ?>
            <barcode_num py:content="'barcode_'+str(barcode_num)" />
            <barcode>
                <filename>${barcode_num}.png</filename>
                <data py:content="barcode_b64" />
            </barcode>
        </page>
    </pages>
</report>

</root>
